<?php
namespace app\index\controller;

use think\facade\Config;
use think\Controller;
use think\facade\Response;

class Base extends Controller
{
    //res
    var $robot_key = null;
    var $user_info = [
        'id' => 'test0001',
        'realname' => 'kitty',
    ];
    var $id = null;
    protected $resp;
    protected $jsdata;
    protected $respbuf = null;
    protected $sn = null;
    protected $paramlist = null;
    protected $argv = null;
    protected $ip = null;
    protected $time = null;

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->time = explode(" ",microtime());
        $this->resp['status'] = 0;
        $this->ip = $this->request->ip();
    }


    function check_params()
    {
        $this->jsdata = [];
        if ($this->request->isPost() && !empty($this->request->post())) {
            $this->jsdata = $this->request->param();
            if (!$this->jsdata || !is_array($this->jsdata)) {
                throw new \MyException(E_PARAMS_ERROR, "invalid params");
            }
        } else if ($this->request->isGet()) {
            $this->jsdata = $this->request->param();
        }

        if (isset($this->jsdata['rsn'])) {
            \RequestContext::$sn = $this->jsdata['rsn'];
        }
        $what = null;
        $this->paramlist['robot_key'] = 'optstring';
        $this->paramlist['robot_id'] = 'optstring';
        $this->paramlist['whosyourdaddy'] = ['optstring', null];

        if (empty($this->jsdata['token'])&&$this->request->has('HTTP_TOKEN','server')) {
            $this->jsdata['token'] = $this->request->server('HTTP_TOKEN');
        }

        if (!isset($this->paramlist['token'])) {
            $this->paramlist['token'] = ['optstring', ''];
        }

        $this->argv = \common::check_json_keys($this->jsdata, $this->paramlist, $what);
        if (!$this->argv) {
            $msg = "输入错误,请确认后重试!";
            $g_env = Config::get('app.runenv');
            if ($g_env != 'ONLINE') {
                $msg = "invailed params,miss " . $what . "!";
            }
            throw_myexception(E_PARAMS_ERROR, $msg);
        }
        $this->argv['pos'] = 0;
        $this->argv['limit'] = 2000;
        if (isset($this->argv['pno'])) {
            $this->argv['pos'] = ($this->argv['pno'] - 1) * $this->argv['psize'];
            if ($this->argv['pos'] < 0) {
                $this->argv['pos'] = 0;
            }
            $this->argv['limit'] = $this->argv['psize'];
            if ($this->argv['limit'] < 0 || $this->argv['limit'] > 2000) {
                $this->argv['limit'] = 2000;
            }
        }
    }

    protected function return_data()
    {
        return json($this->resp);
    }
}

